package cn.tedu.flower.manage.content.controller;

import cn.tedu.flower.manage.common.pojo.vo.PageData;
import cn.tedu.flower.manage.common.security.CurrentPrincipal;
import cn.tedu.flower.manage.common.web.JsonResult;
import cn.tedu.flower.manage.content.pojo.param.MemberAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.MemberUpdateParam;
import cn.tedu.flower.manage.content.pojo.vo.MemberListItemVO;
import cn.tedu.flower.manage.content.pojo.vo.MemberStandardVO;
import cn.tedu.flower.manage.content.service.IMemberService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/content/member")
@Validated
@Api(tags = "1.1. 会员管理-会员信息管理")
public class MemberController {
    @Autowired
    IMemberService memberService;

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/content/article/add-new')")
    @ApiOperation("添加会员")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                             @ApiIgnore HttpServletRequest request,
                             @Valid MemberAddNewParam memberAddNewParam) {
        log.debug("开始处理【发布会员】的请求，参数：{}", memberAddNewParam);
        String remoteAddr = request.getRemoteAddr();
        memberService.addNew(currentPrincipal, remoteAddr, memberAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/content/article/delete')")
    @ApiOperation("根据ID删除会员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会员ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的会员ID值！") Long id) {
        log.debug("开始处理【根据ID删除会员】的请求，参数：{}", id);
        memberService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/put-on")
    @PreAuthorize("hasAuthority('/content/article/update')")
    @ApiOperation("启用会员")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会员ID", required = true, dataType = "long")
    })
    public JsonResult setStatusOn(@PathVariable @Range(min = 1, message = "请提交有效的会员ID值！") Long id) {
        log.debug("开始处理【上架商品】的请求，参数：{}", id);
        memberService.setStatusOn(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/put-off")
    @PreAuthorize("hasAuthority('/content/article/update')")
    @ApiOperation("禁用会员")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会员ID", required = true, dataType = "long")
    })
    public JsonResult setStatusOff(@PathVariable @Range(min = 1, message = "请提交有效的会员ID值！") Long id) {
        log.debug("开始处理【禁用会员】的请求，参数：{}", id);
        memberService.setStatusOff(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/content/article/read')")
    @ApiOperation("根据ID查询会员详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会员ID", required = true, dataType = "long")
    })
    public JsonResult selectById(
            @PathVariable @Range(min = 1, message = "请提交有效的会员ID值！") Long id) {
        log.debug("开始处理【根据ID查询会员详情】的请求，参数：{}", id);
        MemberStandardVO queryResult = memberService.selectById(id);
        return JsonResult.ok(queryResult);
    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/content/article/update')")
    @ApiOperation("修改会员信息")
    @ApiOperationSupport(order = 312)
    public JsonResult update(MemberUpdateParam memberUpdateParam){
        log.debug("开始处理【修改会员信息】的请求，参数：{}",memberUpdateParam);
        memberService.update(memberUpdateParam);
        return JsonResult.ok();


    }

//    @GetMapping("/select/{username}")
//    @PreAuthorize("hasAuthority('/content/article/read')")
//    @ApiOperation("根据用户名查询会员详情")
//    @ApiOperationSupport(order = 411)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
//    })
//    public JsonResult selectByUsername(
//            @PathVariable @Range(min = 1, message = "请提交有效的会员用户名！") String username) {
//        log.debug("开始处理【根据用户名查询会员详情】的请求，参数：{}", username);
//        MemberStandardVO queryResult = memberService.selectByUsername(username);
//        return JsonResult.ok(queryResult);
//    }
//
//    @GetMapping("/select/{nickname}")
//    @PreAuthorize("hasAuthority('/content/article/read')")
//    @ApiOperation("根据昵称查询会员详情")
//    @ApiOperationSupport(order = 412)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "nickname", value = "昵称", required = true, dataType = "String")
//    })
//    public JsonResult selectByNickname(
//            @PathVariable @Range(min = 1, message = "请提交有效的会员昵称！") String nickname) {
//        log.debug("开始处理【根据用户名查询会员详情】的请求，参数：{}", nickname);
//        MemberStandardVO queryResult = memberService.selectByNickname(nickname);
//        return JsonResult.ok(queryResult);
//    }

    @GetMapping("")
    @PreAuthorize("hasAuthority('/content/article/read')")
    @ApiOperation("查询会员列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
    })
    public JsonResult list(@Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        log.debug("开始处理【查询会员列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<MemberListItemVO> pageData = memberService.selectAll(pageNum);
        return JsonResult.ok(pageData);
    }


}
